package smashedapples.forms
{
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.controls.ComboBox;
	
	import smashedapples.utils.ArrayUtils;
	

	public class ComboBox extends mx.controls.ComboBox implements IFormControl
	{
		
		
		public function ComboBox():void
		{
			super();


		}
		private var _cInclude:Boolean = true; 
		public function get cInclude():Boolean
		{
			return _cInclude;
		}
		public function set cInclude( value:Boolean ):void
		{
			_cInclude = value;
		}
		
		public function cReset():void
		{
			this.cValue = this.cDefault;
		}
		
		private var _selected : *;
		public function get selected():String
		{
			return _selected;
		}
		public function set selected(value:*):void
		{
			_selected = value;
			this.cValue = value;
		}
		
		
		
		private var _cProperty : String = "data";
		public function get cProperty():String
		{
			return _cProperty;
		}
		public function set cProperty(cProperty:String):void
		{
			_cProperty = cProperty;
		}
		
		
		
		
		private var _cName : String;
		public function get cName():String
		{
			if( _cName != null)
				return _cName;
			return this.id as String;
		}
		public function set cName(value:String):void
		{
			_cName = value;
		}
		
	
	
		public function get cValue():* 
		{	
			
			if( this.selectedItem == null || this.selectedItem[cProperty] == null)
				return void;
	
			return this.selectedItem[cProperty];
		}
		public function set cValue(value:*):void
		{	
	
			if(value is String)
			{
				if( (value as String).length == 0)
					this.selectedIndex = -1;	
			}
			
			if( this.dataProvider is XMLListCollection)
			{
				for each( var item : XML in (this.dataProvider as XMLListCollection))
				{
					if( value == item.attribute( this.cProperty ) )
					{
						this.selectedItem = item;
					}
				}
			} 
			
			if( this.dataProvider is ArrayCollection )
			{
				var ar:Array = (this.dataProvider as ArrayCollection).source;
				this.selectedIndex = ArrayUtils.getIndexByID(ar, this.cProperty, value);
			}
			
		}
		
		
		private var _cDefault:*;
		public function get cDefault():*
		{
			
		}
		public function set cDefault( value:* ):void
		{
			
		}
		
		private var _cCache:Boolean = false;

		public function get cCache():Boolean
		{
			return _cCache;
		}

		public function set cCache( value:Boolean ):void
		{
			_cCache = value;
		}
		
		
		
		
		
		
	}
}
